/**
 * 
 */
package edu.ou.cs.youming;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Map;

import javax.xml.bind.JAXBException;

import edu.ou.cs.youming.agents.RandomAgent;
import edu.ou.cs.youming.jaxb.types.Example;
import edu.ou.cs.youming.jaxb.types.Feature;
import edu.ou.cs.youming.jaxb.types.Label;
import edu.ou.cs.youming.services.DataIO;
import edu.ou.cs.youming.services.Services;

/**
 * @author Youming Lin
 * 
 */
public class RandomMain {
	static final String corpusPath = "D:/Cruzer USB/Fall 2012/CS 5033/Project/corpus.csv";
	static final String dir = "D:/Cruzer USB/Fall 2012/CS 5033/Project/rawdata/";

	/**
	 * @param args
	 * @throws IOException
	 * @throws JAXBException
	 */
	public static void main(final String[] args) throws IOException, JAXBException {
		// create results directory for saving accuracy results
		final File folder = new File("results");
		folder.mkdir();
		final PrintWriter out = new PrintWriter(new FileWriter(folder.getAbsolutePath()
				+ File.separator + "random.csv"));

		// //////////////////////////////////////////////////////////////////////////////
		// parse data set
		final ArrayList<Example> examples = (ArrayList<Example>) DataIO
				.parseExamples(corpusPath, dir);
		final Iterator<Example> itr = examples.iterator();

		// remove irrelevant and neutral samples
		while (itr.hasNext()) {
			switch (itr.next().polarity) {
				case POSITIVE:
					break;
				case NEGATIVE:
					break;
				default:
					itr.remove();
			}
		}

		final Map<Feature, Label> features = Services.generateFeatures(examples);

		// //////////////////////////////////////////////////////////////////////////////
		// generate random labels
		final RandomAgent agent = new RandomAgent(EnumSet.of(Label.POSITIVE,
				Label.NEGATIVE));

		out.println("Trial, Accuracy");

		for (int i = 0; i < 100; ++i) {
			int correct = 0;

			for (final Example e : examples) {
				correct += (agent.predict(null) == e.polarity) ? 1 : 0;
			}

			out.println((i + 1) + ", " + (double) correct / (double) examples.size()
					* 100.0);
		}

		out.close();
		System.out.println("Done");
	}
}